package com.wendjia.demo.controller;

import com.wendjia.demo.config.DemoProperties;
import com.wendjia.demo.dao.UserDao;
import com.wendjia.demo.model.User;
import com.wendjia.demo.redis.IRedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;

/**
 * Created by wendjia on 2017/1/19.
 */
@RestController
@RequestMapping("/test")
@EnableAutoConfiguration
public class Wendjia {
    @Value("${cupSize}")
    private  String cupSize;

    @Autowired
    private DemoProperties demoProperties;

    @Resource
    private IRedisService redisService;

    @RequestMapping(value="/",method = RequestMethod.GET)
    public String index() {
        return "Hello World~";
    }
    @RequestMapping(value="/wendjia",method = RequestMethod.GET)
    public String wendjia() {
        return "hello wendjia";
    }

    @RequestMapping(value ="/login" )
    public HashMap<String, Object> login(@RequestParam(value = "name" , required = false,defaultValue = "") String name,
                                         @RequestParam(value = "password",required = false,defaultValue = "")String password){
        HashMap<String,Object>  map = new HashMap<>();
        map.put("name",name);
        return  map;
    }
    @RequestMapping(value = "/lelu")
    public HashMap<String,Object> lelu(){
        HashMap<String,Object> map = new HashMap<>();
        map.put("wendjia","wendjia");
        return map;
    }
    @RequestMapping(value = "/user/{user}/repos")
    public HashMap<String,Object> getUser(@PathVariable(name = "user")String  id){
        //todo 通过id找用户
        HashMap<String,Object> map = new HashMap<>();
        if ("wendjia".equals(id)){
            map.put("wendjia","wendjia");
        }else {
            map.put("error","没有此客户");
        }
        return map;
    }

    @RequestMapping(value = "/redis/get")
    public String getName(){
        String name = redisService.get("name");
        return name;
    }
    @RequestMapping(value = "/redis/set")
    public boolean setName(@RequestParam(value = "value",required = false)String value){
        if (value!=null){
            boolean isOk = redisService.set("name", value);
            return isOk;
        }
         return false;
    }

}
